//
//  HJRecommendInformationCell.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJRecommendInformationCell.h"
#import "HJInformationImagesView.h"
#import "HJInformationAuthorView.h"
#import "QMUIButton.h"
#import "YYLabel.h"

@interface HJRecommendInformationCell()

@property (nonatomic, weak) HJInformationAuthorView *informationAuthorView;
@property (nonatomic, weak) HJInformationOperateView *operateView;
@property (nonatomic, weak) HJInformationImagesView *informationImagesView;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIButton *foldButton;

@end

@implementation HJRecommendInformationCell


- (void)settingSubView {
    [super settingSubView];
    

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    HJInformationAuthorView *informationAuthorView = [[HJInformationAuthorView alloc] init];
    [informationAuthorView.moreButton addTarget:self action:@selector(didClickMoreButton:) forControlEvents:UIControlEventTouchUpInside];
    [informationAuthorView.avatarImageView addTapGesturesWithTarget:self action:@selector(tapAvatarImageView)];
    
    UILabel *contentLabel = [UILabel addToView:self.contentView];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.numberOfLines = 0;
    contentLabel.font = UIFontMake(14);
    contentLabel.textColor = UIColor.tj_333333Color;
    _contentLabel = contentLabel;
    
    QMUIButton *foldButton = [QMUIButton buttonWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_mainColor title:nil];
    [foldButton setImagePosition:QMUIButtonImagePositionRight];
    [foldButton setImage:[UIImage imageNamed:@"message_top"] forState:UIControlStateNormal];
    [foldButton setImage:[UIImage imageNamed:@"message_down"] forState:UIControlStateSelected];
    [foldButton setTitle:@"Conceal" forState:UIControlStateNormal];
    [foldButton setTitle:@"All" forState:UIControlStateSelected];
    [foldButton setSpacingBetweenImageAndTitle:4];
    [foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
    _foldButton = foldButton;
    
    HJInformationImagesView *informationImagesView = [[HJInformationImagesView alloc] init];
    TJWeakify(self)
    [informationImagesView setTapImageViewBlock:^(NSInteger index, UIImageView *imageView) {
        if ([weakself.delegate respondsToSelector:@selector(informationCellDidClickImageView:imageView:index:)]) {
            [weakself.delegate informationCellDidClickImageView:weakself imageView:imageView index:index];
        }
    }];
    HJInformationOperateView *operateView = [[HJInformationOperateView alloc] initWithTypes:@[
        @(HJInformationOperateLike),
        @(HJInformationOperateShare),
        @(HJInformationOperateComment),
        @(HJInformationOperateViews),
    ]];
    [operateView setDidClickButtonBlock:^(UIButton * _Nonnull button, HJInformationOperateType type) {
        if ([weakself.delegate respondsToSelector:@selector(informationCellDidClickOperateViewButton:type:button:)]) {
            [weakself.delegate informationCellDidClickOperateViewButton:weakself type:type button:button];
        }
    }];
    UIView *lineView = [UIView addToView:self.contentView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(0.7);
        make.bottom.offset(0);
    }];
    
    [self.contentView addSubview:informationAuthorView];
    [self.contentView addSubview:informationImagesView];
    [self.contentView addSubview:operateView];

    _informationImagesView = informationImagesView;
    _informationAuthorView = informationAuthorView;
    _operateView = operateView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _informationAuthorView.frame = self.layout.authorInfoRect;
    _informationImagesView.frame = self.layout.imagesRect;
    _operateView.frame = self.layout.operateRect;
    _contentLabel.frame = self.layout.contentRect;

    if (self.layout.isShowAllButton) {
        _foldButton.frame = self.layout.foldButtonRect;
    } else {
        _foldButton.frame = CGRectZero;
    }
}

- (void)tapAvatarImageView {
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickTapAvatarImageView:)]) {
        [self.delegate informationCellDidClickTapAvatarImageView:self];
    }
}

- (void)didClickFoldButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickFoldButton:foldButton:)]) {
        [self.delegate informationCellDidClickFoldButton:self foldButton:button];
    }
}

- (void)didClickMoreButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickMoreButton:button:)]) {
        [self.delegate informationCellDidClickMoreButton:self button:button];
    }
}


- (void)setLayout:(HJInformationLayout *)layout {
    _layout = layout;
        
    _foldButton.hidden = !layout.isShowAllButton;
    _foldButton.selected = layout.isFold;
    
    _contentLabel.attributedText = layout.attributedString;
    _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    [_informationAuthorView.avatarImageView tj_setImageWithUrl:layout.model.avatar placeholderImage:[UIImage imageNamed:@"avatar"]];
    _informationAuthorView.nameLabel.text = layout.model.nickname;
    _informationAuthorView.dateLabel.text = layout.model.publishTime;
    [_informationImagesView reset];
    if (_layout.isVideo) {
        _informationImagesView.videos = layout.model.videos;
    } else {
        _informationImagesView.images = layout.model.images;
    }
    
    _operateView.likeButton.selected = layout.model.is_like;
    [_operateView.likeButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.like] forState:UIControlStateNormal];
    [_operateView.shareButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.share] forState:UIControlStateNormal];
    [_operateView.viewsButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.views] forState:UIControlStateNormal];
    [_operateView.commentButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.comment] forState:UIControlStateNormal];
}


@end
